package com.cgx.websocket.config;


import com.cgx.websocket.MyHandshakeInterceptor;
import com.cgx.websocket.MyWebSocketHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;


//配置类
@Configuration
//开启websocket服务
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

	@Override
	public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {

		//注册websocket实现类，指定参数访问地址;allowed-origins="*" 允许跨域
		registry.addHandler(myHandler(), "/webSocket").addInterceptors(myHandshake()).setAllowedOrigins("*");
		//允许客户端使用SockJS
		registry.addHandler(myHandler(), "/sockjs/webSocket").addInterceptors(myHandshake()).withSockJS();

	}

	@Bean
	public MyWebSocketHandler myHandler(){
		return new MyWebSocketHandler();
	}

	@Bean
	public MyHandshakeInterceptor myHandshake(){
		return new MyHandshakeInterceptor();
	}

}
